package pri.zjy.concurrent;

/**
 * @author zhangjy
 * @description 多线程交替打印A、B、C；打印指定次数
 * <p>
 * notify()和wait()
 * @date 2025/5/7 21:38
 */
public class AlternatePrint1 {

    static final Object monitor = new Object();

    static int num = 1;

    static int limit = 10;

    public static void main(String[] args) {

        Thread thread1 = new Thread() {
            @Override
            public void run() {
                synchronized (monitor) {
                    while (num <= limit) {

                        System.out.println(getName() + "---" + num);
                        num++;
                        try {
                            monitor.notify();
                            monitor.wait();

                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        };
        Thread thread2 = new Thread() {
            @Override
            public void run() {
                synchronized (monitor) {
                    while (num <= limit) {

                        System.out.println(getName() + "---" + num);
                        num++;
                        try {
                            monitor.notify();
                            monitor.wait();

                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        };

        thread1.start();
        thread2.start();

    }

}